package cn.yunhe.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;



import cn.yunhe.dao.DeptDao;
import cn.yunhe.entity.Dept;
import cn.yunhe.entity.Emp;
import cn.yunhe.entity.Suffer;
import cn.yunhe.service.DeptService;
import cn.yunhe.service.EmpService;
import cn.yunhe.service.SuffService;
import cn.yunhe.util.DateUtil;

@Controller
public class SuffController {
	@Autowired
    private SuffService suffService;
	private EmpService empService;
	private DeptService deptService;
	
	
	@RequestMapping("/idChaSuff")
	public String chaSuff(HttpServletRequest request,HttpServletResponse response,int sid){
		System.out.println("========================");
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		SuffService suff=    (SuffService) ac.getBean("suffService");
		Suffer suffer=suff.idChaSuff(sid);
	    System.out.println(suffer.getSname()+","+suffer.getDept().getDename()+","+suffer.getEmp().getEname());
	    request.setAttribute("l",suffer);
	    
		
		DeptService dept=   (DeptService) ac.getBean("deptService");
	    List<Dept> list2=dept.chaDept1();
	    
		
		
		request.setAttribute("list2",list2);
		return "gaiSuff";
		
 	}		
	@ResponseBody
	@RequestMapping(value="/getdoctorbydptid",produces="application/json")
	public Object dataselect(int deid){
		System.out.println("成功触发");
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		 EmpService suff1=    (EmpService) ac.getBean("empService");
		 List<Emp> list = suff1.deidChaEmp1(deid);
		for (Emp emp : list) {
			System.out.println(emp.getEid()+"---------------------"+emp.getEname());
		}
		System.out.println("科室id查医师");
		
		return list;
	}
	
	
	@Test
	public void chaAll(){

		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		 EmpService suff1=    (EmpService) ac.getBean("empService");
		 int deid=1;
		 List<Emp> list1 = suff1.deidChaEmp1(deid);
		 for (Emp emp : list1) {
		 
			System.out.println(emp.getEname());
		}
		
		 System.out.println("结束");
		
	}



	@RequestMapping("/insSuff")
	public String insSuff(HttpServletRequest request,HttpServletResponse response){
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		DeptService dept=   (DeptService) ac.getBean("deptService");
		List<Dept> list2=dept.chaDept1();
		request.setAttribute("list2",list2);
		return "addSuff";
		
	}			
@RequestMapping("/addSuff")
public String addSuff(int eid,String sphone,int ssex,int sage,String sname,String saddre,HttpServletRequest request,HttpServletResponse response){
	System.out.println("========================");
	ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
	SuffService  add=  (SuffService) ac.getBean("suffService");
	Suffer suffer=new Suffer();
	suffer.setSname(sname);
	suffer.setSsex(ssex);
	suffer.setSage(sage);
	suffer.setSaddre(saddre);
	suffer.setSphone(sphone);
	suffer.setEid(eid);
	suffer.setSstate(1);
	suffer.setStime(DateUtil.DateTimeTOString(new Date()));
    int num=add.addSuff(suffer);
    System.out.println(num);
    eidChaEmpDept(request,response);
    //request.setAttribute("list",list);

		
	return "chaSuff";
	}	










@RequestMapping("/gaiSuff")
public String gaiSuff(int sid,int eid,String sphone,int ssex,int sage,int sstate,String sname,String saddre,HttpServletRequest request,HttpServletResponse response){
	System.out.println("========================");
	ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
	SuffService  gai=  (SuffService) ac.getBean("suffService");
	Suffer suffer=new Suffer();
	suffer.setSname(sname);
	suffer.setSsex(ssex);
	suffer.setSage(sage);
	suffer.setSaddre(saddre);
	suffer.setSphone(sphone);
	suffer.setEid(eid);
	suffer.setSid(sid);
	suffer.setSstate(sstate);
    int num=gai.gaiSuff(suffer);
    System.out.println(num);
    


    eidChaEmpDept( request, response);
	return "chaSuff";
	}

@RequestMapping("/sidgaiSuffSsrtate")
public String sidgaiSuffSsrtate(int sid,int sstate,int eid,HttpServletRequest request,HttpServletResponse response){
	System.out.println("========================");
	ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
	SuffService  gai=  (SuffService) ac.getBean("suffService");
	Suffer suffer=new Suffer();
	
	suffer.setSid(sid);
	suffer.setSstate(sstate);
    int num=gai.gaiSuff(suffer);
    System.out.println(num);
    
if (eid==0) {
	eidChaEmpDept( request,response);
}else {
	SuffService  SE=  (SuffService) ac.getBean("suffService");
	List<Suffer> list=SE.eidChaSuff(eid);
	
	request.setAttribute("list",list);
	
}
   
   
	return "eidchaSuff";
	}

@RequestMapping("/delSuff")
public String delSuff(HttpServletRequest request,HttpServletResponse response,int sid){
	System.out.println("========================");
	ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
	SuffService  del=  (SuffService) ac.getBean("suffService");
	Suffer suffer=new Suffer();
    suffer.setSid(sid);
    int num=del.delSuff(suffer);
   
    
    eidChaEmpDept(request,response);
    return "chaSuff";
	}

@RequestMapping("/chaSuff")
public String eidChaEmpDept(HttpServletRequest request,HttpServletResponse response){
	System.out.println("========================");
	ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
	SuffService  SE=  (SuffService) ac.getBean("suffService");
	List<Suffer> list=SE.eidChaEmp();
	for (Suffer suffer : list) {
		System.out.println(suffer.getSname()+"--"+suffer.getEmp().getEname()+"---"+suffer.getDept().getDename());
	}
	request.setAttribute("list",list);

		
	return "chaSuff";
   
}
@RequestMapping("/eidchaSuff")
public String eidChaSuff(HttpServletRequest request,HttpServletResponse response,int eid){
	System.out.println(eid+"========================");
	ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
	SuffService  SE=  (SuffService) ac.getBean("suffService");
	List<Suffer> list=SE.eidChaSuff(eid);
	for (Suffer suffer : list) {
		System.out.println(suffer.getSname()+"--"+suffer.getEmp().getEname()+"---"+suffer.getDept().getDename());
	}
	request.setAttribute("list",list);

	
	return "eidchaSuff";
   
}

@RequestMapping("/sidDelSuff")
public String sidDelSuff(HttpServletRequest request,HttpServletResponse response,int sid,int eid){
	System.out.println("========================");
	ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
	SuffService  del=  (SuffService) ac.getBean("suffService");
	Suffer suffer=new Suffer();
    suffer.setSid(sid);
    int num=del.delSuff(suffer);
   
    eidChaSuff(request,response,eid); 
    
    return "eidchaSuff";
	}	
	
	
	public SuffService getSuffService() {
		return suffService;
	}



	public void setSuffService(SuffService suffService) {
		this.suffService = suffService;
	}





	public EmpService getEmpService() {
		return empService;
	}





	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}





	public DeptService getDeptService() {
		return deptService;
	}





	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}	
	
	
}
